<html>
<head>
  <title>BobServer.py</title>
</head>
<body bgcolor="#ffffff" text="#000000">
<pre>
<font color="#008000">&quot;&quot;</font><font color="#008000">&quot;
BobServer.py v0.1
---
This is the first version of the simple Bob Chat Server 
&quot;</font><font color="#008000">&quot;&quot;</font>

<strong>import</strong> <font color="#2040a0">sys</font>, <font color="#2040a0">time</font>
<strong>from</strong> <font color="#2040a0">select</font> <strong>import</strong> <font color="#2040a0">select</font>
<strong>from</strong> <font color="#2040a0">socket</font> <strong>import</strong> <font color="#2040a0">socket</font>, <font color="#2040a0">AF_INET</font>, <font color="#2040a0">SOCK_STREAM</font>

<font color="#2040a0">def</font> <font color="#2040a0">now</font><font color="4444FF">(</font><font color="4444FF">)</font><font color="4444FF">:</font>
    <strong>return</strong> <font color="#2040a0">time</font>.<font color="#2040a0">strftime</font><font color="4444FF">(</font><font color="#008000">&quot;%I:%M:%S%p&quot;</font>,<font color="#2040a0">time</font>.<font color="#2040a0">localtime</font><font color="4444FF">(</font><font color="4444FF">)</font><font color="4444FF">)</font>

<font color="#444444"># Server Constants</font>
<font color="#2040a0">HOSTNAME</font> <font color="4444FF">=</font> <font color="#008000">&quot;&quot;</font> <font color="#444444"># &quot;&quot; == localhost</font>
<font color="#2040a0">PORT</font> <font color="4444FF">=</font> <font color="#FF0000">7777</font>

<font color="#444444"># Make main sockets for accepting new client requests.</font>
<font color="#2040a0">mainsocks</font>, <font color="#2040a0">readsocks</font>, <font color="#2040a0">writesocks</font> <font color="4444FF">=</font> <font color="4444FF">[</font><font color="4444FF">]</font>,<font color="4444FF">[</font><font color="4444FF">]</font>,<font color="4444FF">[</font><font color="4444FF">]</font>

<font color="#444444"># Create an reading socket.</font>
<font color="#2040a0">portsock</font> <font color="4444FF">=</font> <font color="#2040a0">socket</font><font color="4444FF">(</font><font color="#2040a0">AF_INET</font>, <font color="#2040a0">SOCK_STREAM</font><font color="4444FF">)</font>
<font color="#2040a0">portsock</font>.<font color="#2040a0">bind</font><font color="4444FF">(</font><font color="4444FF">(</font><font color="#2040a0">HOSTNAME</font>, <font color="#2040a0">PORT</font><font color="4444FF">)</font><font color="4444FF">)</font>
<font color="#2040a0">portsock</font>.<font color="#2040a0">listen</font><font color="4444FF">(</font><font color="#FF0000">5</font><font color="4444FF">)</font>

<font color="#444444"># A more serious application might open several ports for listening. (like 7777 - 7780)</font>
<font color="#444444"># These lists are used by select.  For now we just have incoming sockets</font>
<font color="#2040a0">mainsocks</font>.<font color="#2040a0">append</font><font color="4444FF">(</font><font color="#2040a0">portsock</font><font color="4444FF">)</font>
<font color="#2040a0">readsocks</font>.<font color="#2040a0">append</font><font color="4444FF">(</font><font color="#2040a0">portsock</font><font color="4444FF">)</font>

<font color="#444444"># Message Queue</font>
<font color="#2040a0">Q</font> <font color="4444FF">=</font> <font color="4444FF">[</font><font color="4444FF">]</font>

<font color="#2040a0">def</font> <font color="#2040a0">ServeIt</font><font color="4444FF">(</font><font color="4444FF">)</font><font color="4444FF">:</font>
    <font color="#444444"># Starting Select Server:</font>
    <font color="#2040a0">done</font> <font color="4444FF">=</font> <font color="#FF0000">0</font>
    <strong>while</strong> <strong>not</strong> <font color="#2040a0">done</font><font color="4444FF">:</font>
        <font color="#444444"># The one and only call to select - There is one missing optional argument</font>
        <font color="#444444"># in the select call which can be used to set a timeout or timeout behavior.</font>
        <font color="#2040a0">readables</font>, <font color="#2040a0">writeables</font>, <font color="#2040a0">exceptions</font> <font color="4444FF">=</font> <font color="#2040a0">select</font><font color="4444FF">(</font><font color="#2040a0">readsocks</font>,<font color="#2040a0">writesocks</font>,<font color="4444FF">[</font><font color="4444FF">]</font><font color="4444FF">)</font>
        <strong>for</strong> <font color="#2040a0">sockobj</font> <strong>in</strong> <font color="#2040a0">readables</font><font color="4444FF">:</font>
            <strong>if</strong> <font color="#2040a0">sockobj</font> <strong>in</strong> <font color="#2040a0">mainsocks</font><font color="4444FF">:</font>
                <font color="#444444"># port socket: accept new client</font>
                <font color="#2040a0">newsock</font>, <font color="#2040a0">address</font> <font color="4444FF">=</font> <font color="#2040a0">sockobj</font>.<font color="#2040a0">accept</font><font color="4444FF">(</font><font color="4444FF">)</font>
                <strong>print</strong> <font color="#008000">'Connect:'</font>, <font color="#2040a0">address</font>, <font color="#2040a0">id</font><font color="4444FF">(</font><font color="#2040a0">newsock</font><font color="4444FF">)</font>
                <font color="#2040a0">readsocks</font>.<font color="#2040a0">append</font><font color="4444FF">(</font><font color="#2040a0">newsock</font><font color="4444FF">)</font>
            <strong>else</strong><font color="4444FF">:</font>
                <font color="#444444"># This is already an open connection, handle it</font>
                <strong>try</strong><font color="4444FF">:</font>
                    <font color="#2040a0">data</font> <font color="4444FF">=</font> <font color="#2040a0">sockobj</font>.<font color="#2040a0">recv</font><font color="4444FF">(</font><font color="#FF0000">1024</font><font color="4444FF">)</font>
                    <strong>print</strong> <font color="#008000">'\tgot'</font>, <font color="#2040a0">data</font>, <font color="#008000">'on'</font>, <font color="#2040a0">id</font><font color="4444FF">(</font><font color="#2040a0">sockobj</font><font color="4444FF">)</font>
                    <strong>if</strong> <strong>not</strong> <font color="#2040a0">data</font><font color="4444FF">:</font>
                        <font color="#2040a0">sockobj</font>.<font color="#2040a0">close</font><font color="4444FF">(</font><font color="4444FF">)</font>
                        <font color="#2040a0">readsocks</font>.<font color="#2040a0">remove</font><font color="4444FF">(</font><font color="#2040a0">sockobj</font><font color="4444FF">)</font>
                        <strong>if</strong> <font color="#2040a0">sockobj</font> <strong>in</strong> <font color="#2040a0">writesocks</font><font color="4444FF">:</font>
                            <font color="#2040a0">writesocks</font>.<font color="#2040a0">remove</font><font color="4444FF">(</font><font color="#2040a0">sockobj</font><font color="4444FF">)</font>
                    <strong>else</strong><font color="4444FF">:</font>
                        <font color="#444444"># Drop message in the Q</font>
                        <font color="#2040a0">Q</font>.<font color="#2040a0">append</font><font color="4444FF">(</font><font color="#2040a0">data</font><font color="4444FF">)</font>
                        <font color="#444444"># Make the socket writable</font>
                        <strong>if</strong> <strong>not</strong> <font color="4444FF">(</font><font color="#2040a0">sockobj</font> <strong>in</strong> <font color="#2040a0">writesocks</font><font color="4444FF">)</font><font color="4444FF">:</font>
                            <font color="#2040a0">writesocks</font>.<font color="#2040a0">append</font><font color="4444FF">(</font><font color="#2040a0">sockobj</font><font color="4444FF">)</font>
                <strong>except</strong><font color="4444FF">:</font>
                    <strong>pass</strong>
        <font color="#444444"># Get ready to write</font>
        <strong>while</strong> <font color="#FF0000">1</font><font color="4444FF">:</font>
            <strong>if</strong> <font color="4444FF">(</font><font color="#2040a0">len</font><font color="4444FF">(</font><font color="#2040a0">writeables</font><font color="4444FF">)</font><font color="4444FF">=</font><font color="4444FF">=</font><font color="#FF0000">0</font><font color="4444FF">)</font> <strong>or</strong> <font color="4444FF">(</font><font color="#2040a0">len</font><font color="4444FF">(</font><font color="#2040a0">Q</font><font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">)</font><font color="4444FF">:</font>
                <strong>break</strong>
            <font color="#2040a0">message</font> <font color="4444FF">=</font> <font color="#2040a0">Q</font><font color="4444FF">[</font><font color="#FF0000">0</font><font color="4444FF">]</font>
            <font color="#444444"># Actually Write Messages</font>
            <strong>for</strong> <font color="#2040a0">sockobj</font> <strong>in</strong> <font color="#2040a0">writeables</font><font color="4444FF">:</font>
                <font color="#444444"># Name(Time): Message</font>
                <strong>try</strong><font color="4444FF">:</font>
                    <font color="#2040a0">sockobj</font>.<font color="#2040a0">send</font><font color="4444FF">(</font> <font color="#008000">&quot;%s(%s): %s&quot;</font> <font color="4444FF">%</font> <font color="4444FF">(</font><font color="#2040a0">id</font><font color="4444FF">(</font><font color="#2040a0">sockobj</font><font color="4444FF">)</font>, <font color="#2040a0">now</font><font color="4444FF">(</font><font color="4444FF">)</font>, <font color="#2040a0">message</font><font color="4444FF">)</font><font color="4444FF">)</font>
                    <strong>print</strong> <font color="#008000">&quot;<font color="#77dd77">\t</font>sent&quot;</font>,<font color="#2040a0">message</font>,<font color="#008000">&quot;to&quot;</font>, <font color="#2040a0">id</font><font color="4444FF">(</font><font color="#2040a0">sockobj</font><font color="4444FF">)</font>
                <strong>except</strong><font color="4444FF">:</font>
                    <strong>pass</strong>
            <font color="#2040a0">Q</font>.<font color="#2040a0">remove</font><font color="4444FF">(</font><font color="#2040a0">message</font><font color="4444FF">)</font>
                

<strong>if</strong> <font color="#2040a0">__name__</font><font color="4444FF">=</font><font color="4444FF">=</font><font color="#008000">&quot;__main__&quot;</font><font color="4444FF">:</font>
    <strong>try</strong><font color="4444FF">:</font>
        <font color="#2040a0">ServeIt</font><font color="4444FF">(</font><font color="4444FF">)</font>
    <strong>finally</strong><font color="4444FF">:</font>
        <font color="#2040a0">portsock</font>.<font color="#2040a0">close</font><font color="4444FF">(</font><font color="4444FF">)</font>
            
            

</pre>
<hr>
syntax highlighted by <a href="http://www.palfrader.org/code2html">Code2HTML</a>, v. 0.9.1
</body>
</html>
